<?php
/**
 * Created by PhpStorm.
 * User: Luo Kai Ge
 * Date: 2019/7/10
 * Time: 23:21
 */

namespace App\Traits;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Concerns\HasAttributes;

/**
 * 只适用于model产生的集合或者模型检测
 * Trait ModelAttributes
 * @package App\Traits
 */
Trait ModelAttributes
{
    use HasAttributes;

    /**
     * 校验model是否包含指定属性
     * @param $model
     * @param array $args
     * @return bool
     */
    public function existAttributes($model, array $args = [])
    {
        if (!($model instanceof \Illuminate\Database\Eloquent\Model) || empty($args)) return false;
        $attributes = $model->getAttributes();
        if (empty($attributes) || !is_array($attributes)) return false;
        foreach ($args as $key) {
            if (!isset($attributes[$key])) {
                return false;
                break;
            }
        }
        return true;
    }

    /**
     * 校验model是否包含指定属性
     * @param $model
     * @param $field
     * @return bool
     */
    public function hasAttribute($model, $field)
    {
        if (!($model instanceof \Illuminate\Database\Eloquent\Model) || !$field || !is_string($field)) return false;
        $attributes = $model->getAttributes();
        return (is_array($attributes) && isset($attributes[$field])) ? true : false;
    }

    /**
     * 校验集合是否为空集合
     * @param $collections
     * @return bool
     */
    public function hasCollections($collections)
    {
        if (!$collections || !($collections instanceof \Illuminate\Database\Eloquent\Collection)) return false;
        return (method_exists($collections,'isNotEmpty') && ($collections->isNotEmpty())) ? true : false;
    }

    /**
     * @param $model
     * @param $namespace
     * @return bool
     */
    public function hasModel($model = null, $namespace = null)
    {
        $namespace = (!$namespace || !is_string($namespace)) ? '\Illuminate\Database\Eloquent\Model' : $namespace;
        if (!$model || !($model instanceof $namespace)) return false;
        return (property_exists($model,'exists') && ($model->exists)) ? true : false;
    }

    /**
     * 获取分页当前页码
     * @param Request $request
     * @param string $pagefield
     * @return int
     */
    public function getPage(Request $request, $pagefield = 'page')
    {
        $page = $request->get($pagefield ,1);
        return (is_numeric($page) && ($page > 1)) ? (int)$page : 1;
    }

    /**
     * 获取分页条数
     * @param Request $request
     * @param string $limitfield
     * @return int
     */
    public function getLimit(Request $request, $limitfield = 'limit')
    {
        $limit = $request->get($limitfield ,1);
        return (is_numeric($limit) && ($limit > 1)) ? (int)$limit : 1;
    }

    /**
     * 计算偏移量
     * @param int $page
     * @param int $limit
     * @return int
     */
    public function getOffset($page = 1, $limit = 1)
    {
        $page = (!is_numeric($page) || ($page < 1)) ? 1 : (int)$page;
        $limit = (!is_numeric($limit) || ($limit < 1)) ? 1 : (int)$limit;
        $offset = ($page - 1) * $limit;
        return (int)$offset;
    }

    /**
     * @param string $imagestr
     * @return array
     */
    public function formatImages(string $imagestr)
    {
        if (!is_string($imagestr) || empty($imagestr) || ctype_space($imagestr)) return [];
        $images = explode(';',$imagestr);
        if (!is_array($images) || empty($images)) return [];
        foreach ($images as $key => &$image) {
            if (empty($image)) {
                unset($images[$key]);
                continue;
            }
            $ext = pathinfo($image,PATHINFO_EXTENSION);
            if (!$ext || !in_array($ext,['jpg','jpeg','png','bmp','gif'])) {
                unset($images[$key]);
                continue;
            }
//            if (preg_match('/(https:\/\/)/i', $image)) {
//                $image = preg_replace('/(https:\/\/)/i', 'http://', $image);
//            }
        }
        return $images ? $images : [];
    }

    /**
     * @param $url
     * @return string|string[]|null
     */
    public function filterProtocol($url)
    {
        if (preg_match('/(http:\/\/)|(https:\/\/)/i', $url)) {
            $url = preg_replace('/(http:\/\/)|(https:\/\/)/i', '', $url);
        }
        return $url;
    }
}
